using ECommerce.Web.DesignByContract;
using ECommerce.Web.NHibernate.Sessions;
using ECommerce.Web.ViewModels;
using NHibernate;

namespace ECommerce.Web.ViewQueries
{
    public interface IProductShowViewQuery
    {
        ProductShowView GetQuery(int productId);
    }

    public class ProductShowViewQuery : IProductShowViewQuery
    {
        public ProductShowView GetQuery(int productId)
        {
            Contract.Require(productId != default(int), "ProductId should not be default value.");

            ISession session = SessionSource.Current.GetSession();

            var view = session.Get<ProductShowView>(productId);

            if (view == null)
            {
                return ProductShowView.Empty;
            }

            return view;
        }
    }
}